//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package requirement_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type LinkIssueInfoDbItem struct {
	IssueId       string `bson:"_id"`
	ProjectId     string `bson:"projectId"`
	RequirementId string `bson:"requirementId"`
	TimeStamp     int64  `bson:"timeStamp"`
}

type _LinkIssueInfoDao struct {
	mc *mongo.Collection
}

func newLinkIssueInfoDao(db *mongo.Database) (*_LinkIssueInfoDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_REQUIRE_MENT + "link_issue")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}, {"requirementId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_LinkIssueInfoDao{mc: mc}, nil
}

func (db *_LinkIssueInfoDao) Insert(ctx context.Context, dbItem *LinkIssueInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_LinkIssueInfoDao) Count(ctx context.Context, projectId, requirementId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"projectId": projectId, "requirementId": requirementId})
	return uint32(count), err
}

func (db *_LinkIssueInfoDao) List(ctx context.Context, projectId, requirementId string) ([]*LinkIssueInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId, "requirementId": requirementId}, options.Find().SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*LinkIssueInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_LinkIssueInfoDao) ListMulti(ctx context.Context, projectId string, requirementIdList []string) ([]*LinkIssueInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId, "requirementId": bson.M{"$in": requirementIdList}},
		options.Find().SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*LinkIssueInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_LinkIssueInfoDao) ListByIssueId(ctx context.Context, projectId string, issueIdList []string) ([]*LinkIssueInfoDbItem, error) {
	query := bson.M{"_id": bson.M{"$in": issueIdList}}
	if projectId != "" {
		query["projectId"] = projectId
	}
	cursor, err := db.mc.Find(ctx, query)
	if err != nil {
		return nil, err
	}
	retList := []*LinkIssueInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_LinkIssueInfoDao) Remove(ctx context.Context, issueId, requirementId string) error {
	query := bson.M{"_id": issueId}
	if requirementId != "" {
		query["requirementId"] = requirementId
	}
	_, err := db.mc.DeleteOne(ctx, query)
	return err
}

func (db *_LinkIssueInfoDao) RemoveByRequirement(ctx context.Context, projectId, requirementId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"projectId": projectId, "requirementId": requirementId})
	return err
}

func (db *_LinkIssueInfoDao) Get(ctx context.Context, issueId, requirementId string) (*LinkIssueInfoDbItem, error) {
	query := bson.M{"_id": issueId}
	if requirementId != "" {
		query["requirementId"] = requirementId
	}
	retItem := &LinkIssueInfoDbItem{}
	err := db.mc.FindOne(ctx, query).Decode(retItem)
	return retItem, err
}

func (db *_LinkIssueInfoDao) Exist(ctx context.Context, issueId, requirementId string) (bool, error) {
	query := bson.M{"_id": issueId}
	if requirementId != "" {
		query["requirementId"] = requirementId
	}
	count, err := db.mc.CountDocuments(ctx, query)
	return count > 0, err
}
